<?php
namespace Home\Controller;

use Think\Controller;

class AgenController extends BaseController
{

    public function order()
    {
        $n = I('n');
        $this->assign('type', $n);
        $this->display();
    }


    /*资格证书*/
    public function credentials()
    {
        $id = parent::get("agen")['id'];
        if (empty($id)) {
            $this->ajaxError("未登录");
        }
        $data = M("Agen")
            ->where(array("id" => $id, "status" => "2"))
            ->field("id,name,level,phone,webchat,idcard,updateTime")
            ->find();
        $dataUpdatetime = strtotime($data['updatetime']);
        $data['updatetime'] = date("Y-m-d", $dataUpdatetime);

        if ($data) {
            return $this->ajaxSuccess($data);
        } else {
            return $this->ajaxError("证书生成失败，请联系管理员！");
        }
    }


    /*代理审核流转记录*/
    public function agenflow()
    {
        //当前登陆的id
        $id = parent::get("agen")['id'];
        if (empty($id)) {
            $this->ajaxError("未登录");
        }
        $page = I('page');
        $list = 20;
        $page2 = $page * $list;
        $data = M('flow')
            ->alias('f')
            ->field('f.*,a.account source,a.name sourcename,b.account `before`,b.name beforename,c.account `after`,c.name aftername')
            ->join('LEFT JOIN 	agenreview v on f.sourceId = v.id')
            ->join('LEFT JOIN 	agen a on v.agenId = a.id')
            ->join('LEFT JOIN 	agen b on f.beforeId = b.id')
            ->join('LEFT JOIN 	agen c on f.afterId = c.id')
            ->where("f.sourceType = 2 and  (f.beforeId = $id OR f.afterId = $id)")
            ->order('f.createTime desc')
            ->limit($page2, $list)
            ->select();
        if ($data) {
            $this->ajaxSuccess($data);
        } else {
            $this->ajaxError("没有你要的数据");
        }
    }

    /*代理下单流转记录*/
    public function onlineflow()
    {
        //当前登陆的id
        $id = parent::get("agen")['id'];
        if (empty($id)) {
            $this->ajaxError("未登录");
        }
        $page = I('page');
        $list = 20;
        $page2 = $page * $list;
        $data = M('flow')
            ->alias('f')
            ->field('f.*,a.account `before`,a.name beforename,b.account `after`,b.name aftername,o.sn ')
            ->join('agen a ON a.id = f.beforeId')
            ->join('agen b ON b.id = f.afterId')
            ->join("agenorder o ON f.sourceId = o.id")
            ->where("f.sourceType=1 AND (f.beforeId = $id OR f.afterId = $id)")
            ->order('f.createTime desc')
            ->limit($page2, $list)
            ->select();
        if ($data) {
            $this->ajaxSuccess($data);
        } else {
            $this->ajaxError("没有你要的数据");
        }
    }

    //查询我的客户信息
    public function myCustomer()
    {
        //获取登录信息
        $id = parent::get("agen")['id'];
        if (empty($id)) {
            $this->ajaxError("未登录");
        }
        $page = I('page');
        $list = 20;
        $page2 = $page * $list;
        $customer = M('member')
            ->alias('m')
            ->join('LEFT JOIN 
            ( SELECT SourceId AS id,count(id) AS counts,sum(total) AS sum
                FROM onlineorder
                WHERE orderType = 1 AND `status` in (2,3,4)
                GROUP BY SourceId
            ) o ON m.id = o.id')
            ->where("m.belongAgenId = $id")
            ->field('m.`name`,m.account,IFNULL(o.counts,0) counts,IFNULL(o.sum,0) sum')
            ->limit($page2, $list)
            ->select();
        if ($customer) {
            $this->ajaxSuccess($customer);
        } else {
            $this->ajaxError("您还没有客户");
        }
    }

    //代理商资料页面
    public function getAgenInfo()
    {
        //获取登录信息
        $uid = parent::get("agen")['id'];
        if (empty($uid)) {
            $this->ajaxError("未登录");
        }
        if (empty($uid)) {
            $this->ajaxError("用户未登录！");
        }
        $agenInfo = M("agen")
            ->where("id = $uid and status = 2 and isDisable = 2")
            ->find();
        if (empty($agenInfo)) {
            $this->ajaxError("用户未登录！");
        }

        $agenInfo = M('agen')
            ->alias('a')
            ->join("LEFT JOIN region r on r.id = a.regionId")
            ->join("LEFT JOIN (SELECT * FROM agenaddress WHERE isDefault = 1 AND isDelete = 2) aa ON aa.agenId = a.id")
            ->join("LEFT JOIN region r2 on r2.id = aa.regionId")
            ->join("LEFT JOIN agen ua on ua.id = a.upagenid")
            ->join("LEFT JOIN agen ra on ra.id = a.recommendId")
            ->field("a.*,r.id_path,r.name_path,aa.id addressid,aa.regionId,aa.address,r2.name_path reveiveaddress,aa.name receivename,aa.phone receivephone,ua.name uaname,ua.phone uaphone,ra.name raname,ra.phone raphone")
            ->where("a.id = $uid")
            ->find();
        $agenInfo['allagenurl'] = $agenInfo['agenurl'];
        $agenInfo['agenurl'] = explode("=", $agenInfo['agenurl'])[1];
        $addres_arr = $arr = explode(",", $agenInfo['id_path']);
        $city_id = $addres_arr[1];
        $area_id = $addres_arr[2];
        $address['province_id'] = $addres_arr[0];
        $address['city_id'] = $city_id;
        $address['area_id'] = $area_id;
        $addresinfo = $arr = explode("/", $agenInfo['name_path']);

        return $this->ajaxSuccess(array("message" => $agenInfo, "city_id" => $city_id, "area_id" => $area_id, "city" => getCity($addres_arr[0]), "area" => getCity($city_id), "province" => getCity(0), 'addresinfo' => $addresinfo));
    }

    //更新代理商资料
    public function updateAgen()
    {
        //获取登录信息
        $agen = parent::get("agen");
        if (empty($agen)) {
            $this->ajaxError("未登录！");
        }
        $id = $agen['id'];
        //获取页面参数
        $name = I('post.name');
        $webchat = I('post.webchat');
        $oldWebchat = $agen['webchat'];
        $phone = I('post.phone');
        $oldPhone = $agen['phone'];
        /*$regionId = I('post.newregionid');
        if(empty($regionId)){
            $xianaddress = I("post.xianaddress");
            $regionaddress = explode("/", trim($xianaddress));
            $prov = mb_substr($regionaddress[0],0,2,'utf-8');
            $city = mb_substr($regionaddress[1],0,2,'utf-8');
            $area = mb_substr($regionaddress[2],0,2,'utf-8');
            $region = M('region')
                ->where("`name_path` LIKE '$prov%' AND `name_path` LIKE '%$city%' AND `name_path` LIKE '%$area%'")
                ->find();
            $regionId = $region['id'];
        }*/
        $updateTime = now_datetime();
        $updateId = $id;

        //姓名验证
        if (!empty($name) && $name != null) {
            $data['name'] = $name;
        } else {
            $this->ajaxError("姓名不能为空！");
        }
        //微信号验证
        if (!empty($webchat) && $webchat != null) {
            //微信号改变则修改
            if ($webchat != $oldWebchat) {
                if (isWebchat($webchat)) {
                    //检测重复
                    $agenBean = M("agen")
                        ->where(array("webchat" => $webchat))
                        ->find();
                    if (!empty($agenBean)) {
                        $this->ajaxError("此微信号已存在！");
                    }
                    $data['webchat'] = $webchat;
                } else {
                    $this->ajaxError("微信号格式不正确！");
                }
            }
        } else {
            $this->ajaxError("微信号不能为空！");
        }
        //手机号验证
        if (!empty($phone) && $phone != null) {
            //手机号改变则修改
            if ($phone != $oldPhone) {
                if (isPhone($phone)) {
                    //检测重复
                    $agenBean = M("agen")
                        ->where(array("phone" => $phone))
                        ->find();
                    if (!empty($agenBean)) {
                        $this->ajaxError("此手机号已存在！");
                    }
                    $data['phone'] = $phone;
                } else {
                    $this->ajaxError("手机号格式不正确！");
                }
            }
        } else {
            $this->ajaxError("手机号不能为空！");
        }

        //经纬度
        $lnglat = parent::getLngLat();
        $memberlng = $lnglat[0];
        $memberlat = $lnglat[1];
        $data['lng'] = $memberlng;
        $data['lat'] = $memberlat;
        //代理地区
        //$data["regionId"] = $regionId;

        //更新
        $User = M("agen");
        $data['updateTime'] = $updateTime;
        $data['updateId'] = $updateId;
        $up = $User->where(array('id' => $id))->save($data);

        if ($up) {
            $agenInfo = M('agen')
                ->alias('a')
                ->join("LEFT JOIN region r on r.id = a.regionId")
                ->join("LEFT JOIN (SELECT * FROM agenaddress WHERE isDefault = 1 AND isDelete = 2) aa ON aa.agenId = a.id")
                ->field("a.*,r.id_path,r.name_path,aa.id addressid,aa.regionId,aa.address,aa.name receivename,aa.phone receivephone")
                ->where("a.id = $updateId")
                ->find();
            $agenInfo['agenurl'] = explode("=", $agenInfo['agenurl'])[1];
            parent::set('agen', $agenInfo);
            $this->ajaxSuccess(array("msg" => "更新资料成功！", "agen" => $agenInfo));
        } else {
            $this->ajaxError(array("msg" => "更新资料失败！"));
        }
    }

    /*我的收款和出款*/
    public function myallbrokerage()
    {
        $id = parent::get("agen")['id'];
        if (empty($id)) {
            $this->ajaxError("未登录");
        }
        $page = I('currentPage');
        $list = 20;
        $page2 = $page * $list;
        //判断是否有查询条件
        $serchflg = 0;
        //时间检索
        $begin_time = I("post.begin_time");
        if (!isset($begin_time) || $begin_time == null) {
            $begin_time = "1970-01-01";
        }
        $end_time = I("post.end_time");
        if (!isset($end_time) || $end_time == null) {
            $end_time = date("Y-m-d");
            $end_time = date('Y-m-d', strtotime("$end_time + 1 day"));
        }
        //代理检索
        $chanchu = I("post.chanchu");
        $shoukuan = I("post.shoukuan");

        if (!isset($chanchu) || $chanchu == null) {
            $chanchu = '%%';
        }
        if (!isset($shoukuan) || $shoukuan == null) {
            $shoukuan = '%%';
        } else {
            $serchflg = 1;
        }
        // 状态
        $statuss = I("slctyp");
        if ($statuss == 0 || $statuss == '') {
            $statuss = '1,2,3';
        }
        // 分类 收款或出款
        $typeid = I('typeid');
        //$data = array();
        if ($typeid == 1) {
            $data = M('difference')
                ->alias('d')
                ->field("d.id, d.type, d.money, d.createTime, d.updateTime, d.sourceId, d.`status`, dd.val `statusname`,a.account arrears,b.account account,c.account source")
                ->join('agen a on d.arrearsId = a.id')
                ->join('agen b on d.accountId = b.id')
                ->join('dropdown dd on d.`status` = dd.`code`')
                ->join('onlineorder o on o.id = d.sourceId')
                ->join('agen c on o.SourceId = c.id')
                ->where("d.arrearsId = $id AND dd.module = 'differenceStatus' AND d.`status` in ($statuss) AND d.createTime >= '$begin_time' AND d.createTime < '$end_time' AND c.account LIKE '$chanchu' AND b.account LIKE '$shoukuan'")
                ->order('d.createTime desc')
                ->limit($page2, $list)
                ->select();

            if ($serchflg == 1) {
                $iddata = M('difference')
                    ->alias('d')
                    ->field("d.id, d.type, d.money")
                    ->join('agen a on d.arrearsId = a.id')
                    ->join('agen b on d.accountId = b.id')
                    ->join('onlineorder o on o.id = d.sourceId')
                    ->join('agen c on o.SourceId = c.id')
                    ->where("d.arrearsId = $id AND d.`status` = 1 AND d.createTime >= '$begin_time' AND d.createTime < '$end_time' AND c.account LIKE '$chanchu' AND b.account LIKE '$shoukuan'")
                    ->select();
            }
        } else if ($typeid == 2) {
            $data = M('difference')
                ->alias('d')
                ->field("d.id, d.type, d.money, d.createTime, d.updateTime, d.sourceId, d.`status`, dd.val `statusname`,a.account arrears,b.account account,c.account source")
                ->join('agen a on d.arrearsId = a.id')
                ->join('agen b on d.accountId = b.id')
                ->join('dropdown dd on d.`status` = dd.`code`')
                ->join('onlineorder o on o.id = d.sourceId')
                ->join('agen c on o.SourceId = c.id')
                ->where("d.accountId = $id AND dd.module = 'differenceStatus' AND d.`status` in ($statuss) AND d.createTime >= '$begin_time' AND d.createTime < '$end_time' AND c.account LIKE '$chanchu' AND a.account LIKE '$shoukuan'")
                ->order('d.createTime desc')
                ->limit($page2, $list)
                ->select();

            if ($serchflg == 1) {
                $iddata = M('difference')
                    ->alias('d')
                    ->field("d.id, d.type, d.money")
                    ->join('agen a on d.arrearsId = a.id')
                    ->join('agen b on d.accountId = b.id')
                    ->join('onlineorder o on o.id = d.sourceId')
                    ->join('agen c on o.SourceId = c.id')
                    ->where("d.accountId = $id AND d.`status` = 2 AND d.createTime >= '$begin_time' AND d.createTime < '$end_time' AND c.account LIKE '$chanchu' AND a.account LIKE '$shoukuan'")
                    ->order('d.createTime desc')
                    ->limit($page2, $list)
                    ->select();
            }
        }
        //有查询条件，获取总价和idlist
        $total = 0;
        if ($serchflg == 1) {
            $idlist = [];
            for ($i = 0; $i < sizeof($iddata); $i++) {
                $idlist[$i] = $iddata[$i]['id'];
                $total += $iddata[$i]['money'];
            }
        }

        if ($shoukuan == '%%') $shoukuan = '';
        return $this->ajaxSuccess(array('skck' => $data, 'total' => $total, 'dl' => $shoukuan, 'type' => $typeid, 'idlist' => $idlist));
    }

    /*确认转账*/
    public function verifyBrokerage()
    {
        $agenid = parent::get("agen")['id'];
        if (empty($agenid)) {
            $this->ajaxError("未登录");
        }
        $id = I("id");
        $result = M("difference")
            ->where(array("id" => $id, 'arrearsId' => $agenid))
            ->save(array("status" => 2, 'updateTime' => date("Y-m-d H:i:s")));
        if ($result) {
            return $this->ajaxSuccess("转账成功！");
        } else {
            return $this->ajaxError("转账失败！");
        }
    }

    //确认完成
    public function verifyBrokerage2()
    {
        $agenid = parent::get("agen")['id'];
        if (empty($agenid)) {
            $this->ajaxError("未登录");
        }
        $id = I("id");
        $result = M("difference")
            ->where(array("id" => $id, 'accountId' => $agenid))
            ->save(array("status" => 3, 'updateTime' => date("Y-m-d H:i:s")));
        if ($result) {
            return $this->ajaxSuccess("确认成功！");
        } else {
            return $this->ajaxError("确认失败！");
        }
    }

    //全部转账
    public function allVerify()
    {
        $agenid = parent::get("agen")['id'];
        if (empty($agenid)) {
            $this->ajaxError("未登录");
        }
        $li = I('skdl');
        if (!isset($li) || $li == "undefined" || $li == "") {
            return $this->ajaxError("请填写收款代理并重新查询后确认！");
        }
        $list = i('idlist');
        $pos = strpos($list, ",");
        if ($pos > 0) {
            $list = explode(',', $list);
        }
        for ($i = 0; $i < sizeof($list); $i++) {
            $id = $list[$i];;
            $result = M("difference")
                ->where(array("id" => $id, 'arrearsId' => $agenid))
                ->save(array("status" => 2, 'updateTime' => date("Y-m-d H:i:s")));
            if (!$result) {
                return $this->ajaxError("转账失败！");
            }
        }
        return $this->ajaxSuccess("转账成功！");
    }

    //全部完成
    public function allVerify2()
    {
        $agenid = parent::get("agen")['id'];
        if (empty($agenid)) {
            $this->ajaxError("未登录");
        }
        $li = I('skdl');
        if (!isset($li) || $li == "undefined" || $li == "") {
            return $this->ajaxError("请填写出款代理并重新查询后确认！");
        }
        $list = i('idlist');
        $pos = strpos($list, ",");
        if ($pos > 0) {
            $list = explode(',', $list);
        }
        for ($i = 0; $i < sizeof($list); $i++) {
            $id = $list[$i];;
            $result = M("difference")
                ->where(array("id" => $id, 'accountId' => $agenid))
                ->save(array("status" => 3, 'updateTime' => date("Y-m-d H:i:s")));
            if (!$result) {
                return $this->ajaxError(array("确认失败！"));
            }
        }
        return $this->ajaxSuccess("确认成功！");
    }

    //准备代理升级页面信息
    public function getAgenUpInfo()
    {
        $level = I('level');
        if (empty($level)) {
            $level = parent::get('agen')['level'];
        }
        if (empty($level)) {
            $this->ajaxError('用户未登录！');
        }
        if ($level == 1) {
            $this->ajaxError('您已达到最高级别，无法升级！');
        }
        //等级对应金额和名称
        $agenlevel = M("agenlevel")
            ->where("level < $level")
            ->select();
        $level = $level - 1;
        $limitMoney = 0;
        foreach ($agenlevel as $al => $value) {
            if ($agenlevel[$al]['level'] == $level) {
                $limitMoney = $agenlevel[$al]['money'];
            }
        }
        //商品列表
        $products = M("product")
            ->where('isUP = 1 and isDelete = 2')
            ->select();
        //组合列表
        $productList = array();
        foreach ($products as $product) {
            $newProduct = array($product["id"] => array(
                'id' => $product["id"],
                'name' => $product["name"],
                'img' => $product['img'],
                'price' => $product['price'],
                'price1' => $product['price1'],
                'price2' => $product['price2'],
                'price3' => $product['price3'],
                'price4' => $product['price4'],
                'price5' => $product['price5'],
                'price6' => $product['price6'],
                'price7' => $product['price7'],
                'price8' => $product['price8'],
                'price9' => $product['price9'],
                'price10' => $product['price10'],
                'deductible' => $product['deductible'],
                'bonus' => $product['bonus'],
                'bonus2' => $product['bonus2'],
                'introduce' => $product['introduce'],
                'boxnum' => $product['boxnum'],
                'myprice' => $product['price' . $level],
                'prozong' => 0,
                'num' => 0
            ));
            foreach ($newProduct as $key => $value) {
                $productList[$key] = $value;
            }
        }
        //自动根据所选等级分配商品数量
        $prototal = 0;
        while (1) {
            foreach ($productList as $key => $value) {
                $prototal += $productList[$key]['myprice'];
                $productList[$key]['prozong'] += $productList[$key]['myprice'];
                $productList[$key]['num']++;
                if ($prototal >= $limitMoney) {
                    break;
                }
            }
            if ($prototal >= $limitMoney) {
                break;
            }
        }
        parent::set('prolist', $productList);

        $this->ajaxSuccess(array('prolist' => $productList, 'prototal' => $prototal, 'level' => $level, 'limitmoney' => $limitMoney, 'agenlevel' => $agenlevel));
    }

    //选择代理等级变更，重新获取商品信息
    public function changelevel()
    {
        $level = I('level');
        //等级对应金额和名称
        $agenlevel = M("agenlevel")
            ->where(array('level' => $level))
            ->find();
        $limitMoney = $agenlevel['money'];

        //商品列表
        $products = M("product")
            ->where('isUP = 1 and isDelete = 2')
            ->select();
        //组合列表
        $productList = array();
        foreach ($products as $product) {
            $newProduct = array($product["id"] => array(
                'id' => $product["id"],
                'name' => $product["name"],
                'img' => $product['img'],
                'price' => $product['price'],
                'price1' => $product['price1'],
                'price2' => $product['price2'],
                'price3' => $product['price3'],
                'price4' => $product['price4'],
                'price5' => $product['price5'],
                'price6' => $product['price6'],
                'price7' => $product['price7'],
                'price8' => $product['price8'],
                'price9' => $product['price9'],
                'price10' => $product['price10'],
                'deductible' => $product['deductible'],
                'bonus' => $product['bonus'],
                'bonus2' => $product['bonus2'],
                'introduce' => $product['introduce'],
                'boxnum' => $product['boxnum'],
                'myprice' => $product['price' . $level],
                'prozong' => 0,
                'num' => 0
            ));
            foreach ($newProduct as $key => $value) {
                $productList[$key] = $value;
            }
        }
        //自动根据所选等级分配商品数量
        $money = I('money');
        if (!empty($money)) {
            $limitMoney = $money;
        }
        $prototal = 0;
        while (1) {
            foreach ($productList as $key => $value) {
                $prototal += $productList[$key]['myprice'];
                $productList[$key]['prozong'] += $productList[$key]['myprice'];
                $productList[$key]['num']++;
                if ($prototal >= $limitMoney) {
                    break;
                }
            }
            if ($prototal >= $limitMoney) {
                break;
            }
        }
        parent::set('prolist', $productList);

        $this->ajaxSuccess(array('prolist' => $productList, 'prototal' => $prototal, 'limitmoney' => $limitMoney));
    }

    //准备报单-购买商品页面
    public function getAgenBuyInfo()
    {
        $regagen = parent::get('agen');

        if (empty($regagen)) {
            $this->ajaxError('未登录！');
        }

        $level = $regagen['level'];
        //等级对应金额和名称
        $agenlevel = M("agenlevel")
            ->where("level = $level")
            ->find();
        $levelname = $agenlevel['name'];
        //报单最低拿货金额
        $reportMoney = $agenlevel['reportmoney'];

        //商品列表
        $products = M("product")
            ->where('isUP = 1 and isDelete = 2')
            ->select();
        //组合列表
        $productList = array();
        foreach ($products as $product) {
            $newProduct = array($product["id"] => array(
                'id' => $product["id"],
                'name' => $product["name"],
                'img' => $product['img'],
                'price' => $product['price'],
                'price1' => $product['price1'],
                'price2' => $product['price2'],
                'price3' => $product['price3'],
                'price4' => $product['price4'],
                'price5' => $product['price5'],
                'price6' => $product['price6'],
                'price7' => $product['price7'],
                'price8' => $product['price8'],
                'price9' => $product['price9'],
                'price10' => $product['price10'],
                'deductible' => $product['deductible'],
                'bonus' => $product['bonus'],
                'bonus2' => $product['bonus2'],
                'introduce' => $product['introduce'],
                'boxnum' => $product['boxnum'],
                'myprice' => $product['price' . $level],
                'prozong' => 0,
                'num' => 0
            ));
            foreach ($newProduct as $key => $value) {
                $productList[$key] = $value;
            }
        }
        //自动根据所选等级分配商品数量
        $money = I('money');
        if (!empty($money)) {
            $reportMoney = $money;
        }
        $prototal = 0;
        while (1) {
            foreach ($productList as $key => $value) {
                $prototal += $productList[$key]['myprice'];
                $productList[$key]['prozong'] += $productList[$key]['myprice'];
                $productList[$key]['num']++;
                if ($prototal >= $reportMoney) {
                    break;
                }
            }
            if ($prototal >= $reportMoney) {
                break;
            }
        }

        $this->ajaxSuccess(array('prolist' => $productList, 'prototal' => $prototal, 'level' => $level, 'levelname' => $levelname, 'minmoney' => $reportMoney));
    }

    /*修改密码*/
    public function editpwd()
    {
        if (IS_POST) {
            $id = parent::get("agen")['id'];
            if (empty($id)) {
                $this->ajaxError("未登录");
            }
            $oldpwd = I('oldpwd');
            $newpwd1 = I('newpwd1');
            $newpwd2 = I('newpwd2');

            if (strlen($newpwd1) < 6) {
                return $this->ajaxError('密码长度必须大于6位');
            }
            if (!isNumAndLetter($newpwd1)) {
                $this->ajaxError("密码必须同时包含数字和英文！");
            }
            if ($newpwd1 != $newpwd2) {
                return $this->ajaxError("两次密码输入不一致！");
            }
            $result = M('agen')
                ->where(array('id' => $id, 'password' => md5($oldpwd)))
                ->find();
            if ($result) {
                $res = M('agen')
                    ->where(array('id' => $id))
                    ->save(array('password' => md5($newpwd1)));
                if ($res) {

                    return $this->ajaxSuccess("修改密码成功");
                } else {
                    return $this->ajaxError('修改密码失败');
                }
            } else {
                return $this->ajaxError('原密码错误');
            }
        }
    }


    //统计代理下级会员、代理的消费
    public function getAgenSubdntCount()
    {
        $agenid = parent::get('agen')['id'];
        if (empty($agenid)) {
            $this->ajaxError('未登录！');
        }
        $begintime = I('post.begintime');
        $endtime = I('post.endtime');
        if (empty($begintime)) {
            $begintime = "1970-01-01 08:00:00";
        }
        if (empty($endtime)) {
            $endtime = "9999-00-00 00:00:00";
        }

        //会员订单总金额
        $memberTotal = M('onlineorder')
            ->where("toagenId = $agenid AND orderType = 1 AND (`status` = 3 OR `status` = 4) AND updateTime >= '$begintime' AND updateTime <= '$endtime'")
            ->field('IFNULL(sum(total),0) total,IFNULL(count(total),0) count')
            ->find();

        //代理报单总金额
        $agenOnlineTotal = M('onlineorder')
            ->alias('o')
            ->join('left join agen a on o.SourceId = a.id')
            ->where("a.upagenid= $agenid AND o.SourceId != $agenid AND o.orderType = 3 AND o.`status` = 3 AND o.updateTime >= '$begintime' AND o.updateTime <= '$endtime'")
            ->field('IFNULL(sum(o.total),0) total,IFNULL(count(o.total),0) count')
            ->find();

        //代理下单总金额
        $agenOrderTotal = M('agenorder')
            ->alias('o')
            ->join('left join agen a on o.SourceId = a.id')
            ->where("a.upagenid= $agenid AND o.SourceId != $agenid AND (o.`status` = 3 OR o.`status` = 4) AND o.updateTime >= '$begintime' AND o.updateTime <= '$endtime'")
            ->field('IFNULL(sum(o.total),0) total,IFNULL(count(o.total),0) count')
            ->find();

        if ($memberTotal && $agenOnlineTotal && $agenOrderTotal) {
            $this->ajaxSuccess(array("memberTotal" => $memberTotal, "agenOnlineTotal" => $agenOnlineTotal, "agenOrderTotal" => $agenOrderTotal, "msg" => "获取数据成功！"));
        } else {
            $this->ajaxError(array("msg" => "获取数据失败！"));
        }
    }

    /*公告*/
    public function notice()
    {
        $notice = getAgenNotice();
        return $this->ajaxSuccess($notice);
    }
}
